<template>
	<div>
		<el-card class="!border-none mb-4" shadow="never">
			<el-form class="mb-[-16px]" :model="queryParams" inline>
				<el-form-item label="用户信息" prop="user_id">
					<el-input class="w-[280px]" v-model="queryParams.user_info" clearable placeholder="账号/邀请码" />
				</el-form-item>
				<el-form-item label="姓名" prop="card_name">
					<el-input class="w-[280px]" v-model="queryParams.card_name" clearable placeholder="请输入姓名" />
				</el-form-item>
				<el-form-item label="证件号" prop="card_num">
					<el-input class="w-[280px]" v-model="queryParams.card_num" clearable placeholder="请输入证件号" />
				</el-form-item>
				<el-form-item label="认证状态" prop="auth_card">
					<el-select class="w-[280px]" v-model="queryParams.auth_card" clearable placeholder="请选择认证状态">
						<el-option label="全部" value=""></el-option>
						<el-option v-for="(item, index) in dictData.auth_status" :key="index" :label="item.name"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="提交时间" prop="create_time">
					<daterange-picker v-model:startTime="queryParams.start_time"
						v-model:endTime="queryParams.end_time" />
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none" v-loading="pager.loading" shadow="never">
			<div class="mt-4">
				<el-table :data="pager.lists" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" />
					<el-table-column label="用户" min-width="150">
						<template #default="{ row }">
							<div>账号：{{row.mobile}}</div>
							<div>邀请码：{{row.u_sn}}</div>
						</template>
					</el-table-column>
					<el-table-column label="姓名" prop="card_name" show-overflow-tooltip />
					<el-table-column label="证件号" prop="card_num" show-overflow-tooltip />
					<el-table-column label="证件正面" min-width="100">
						<template #default="{ row }">
							<image-contain v-if="row.card_img1" :src="row.card_img1" :width="60" :height="45"
								:preview-src-list="[row.card_img1]" preview-teleported fit="contain" />
						</template>
					</el-table-column>
					<el-table-column label="证件反面" min-width="100">
						<template #default="{ row }">
							<image-contain v-if="row.card_img2" :src="row.card_img2" :width="60" :height="45"
								:preview-src-list="[row.card_img2]" preview-teleported fit="contain" />
						</template>
					</el-table-column>
					<el-table-column label="证件手持" min-width="100">
						<template #default="{ row }">
							<image-contain v-if="row.card_img3" :src="row.card_img3" :width="60" :height="45"
								:preview-src-list="[row.card_img3]" preview-teleported fit="contain" />
						</template>
					</el-table-column>
					<el-table-column label="认证状态" prop="auth_card">
						<template #default="{ row }">
							<dict-value :options="dictData.auth_status" :value="row.auth_card" />
						</template>
					</el-table-column>
					<el-table-column label="提交时间" prop="card_time" min-width="120">
						<template #default="{ row }">
							<span>{{ row.card_time ? timeFormat(row.card_time, 'yyyy-mm-dd hh:MM:ss') : '' }}</span>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="120" fixed="right">
						<template #default="{ row }">
							<el-button v-if="row.auth_card == 2" v-perms="['user.user_info/agree']"
								type="primary" link @click="handleAgree(row.id)">
								同意
							</el-button>
							<el-button v-if="row.auth_card == 2" v-perms="['user.user_info/refuse']"
								type="primary" link @click="handleRefuse(row.id)">
								拒绝
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
	</div>
</template>

<script lang="ts" setup name="userInfoLists">
	import { usePaging } from '@/hooks/usePaging'
	import { useDictData } from '@/hooks/useDictOptions'
	import { apiUserInfoLists, apiUserInfoAgree, apiUserInfoRefuse } from '@/api/user_info'
	import { timeFormat } from '@/utils/util'
	import feedback from '@/utils/feedback'


	// 查询条件
	const queryParams = reactive({
		user_info: '',
		email: '',
		google_key: '',
		card_name: '',
		card_num: '',
		auth_email: '',
		auth_google: '',
		auth_card: '',
		start_time: '',
		end_time: ''
	})

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 获取字典数据
	const { dictData } = useDictData('auth_status')

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiUserInfoLists,
		params: queryParams
	})

	// 同意
	const handleAgree = async (id : number | any[]) => {
		await feedback.confirm('确定要同意？')
		await apiUserInfoAgree({ id })
		getLists()
	}

	// 拒绝
	const handleRefuse = async (id : number | any[]) => {
		const remarkVal = await feedback.prompt("", '确定要拒绝？', { inputPlaceholder: '备注（选填）' })
		let remark = '';
		if (remarkVal.value != null) remark = remarkVal.value;
		await apiUserInfoRefuse({ "id": id, "remark": remark })
		getLists()
	}

	getLists()
</script>